package cn.com.headfree.version.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.entity.isc.User;
import cn.com.headfree.version.dao.IVersionDao;
import cn.com.headfree.vo.version.VersionVo;

@Repository("versionDao")
public class VersionDaoImpl extends BaseDaoImpl<VersionVo> implements IVersionDao {

	@Override
	public List<VersionVo> getVersions(String versionNo) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		StringBuilder sqlBuilder = new StringBuilder("select id,content,version_no,pub_time,update_content from ");
		sqlBuilder.append(" t_version where del_sign=0 ");
		if (!StringUtil.isNullOrEmpty(versionNo)) {
			sqlBuilder.append(" and version_no=? ");
			arrayList.add(versionNo);
		}
		sqlBuilder.append(" order by pub_time desc ");
		return this.findAllBySQL(VersionVo.class, sqlBuilder.toString(), null, false, arrayList.toArray());
	}

	@Override
	public void addVersion(VersionVo versionVo) {
		String sql = "insert into t_version (id,content,version_no,pub_time,update_content,creater_id,creater_name,create_time,updater_id,updater_name,update_time,del_sign) values ";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{content,jdbcType=VARCHAR},")
				.append("#{version_no,jdbcType=VARCHAR},").append("#{pub_time,jdbcType=DATE},")
				.append("#{update_content,jdbcType=VARCHAR},").append("#{creater_id,jdbcType=VARCHAR},")
				.append("#{creater_name,jdbcType=VARCHAR},").append("#{create_time,jdbcType=DATE},")
				.append("#{updater_id,jdbcType=VARCHAR},").append("#{updater_name,jdbcType=VARCHAR},")
				.append("#{update_time,jdbcType=DATE},").append("#{del_sign,jdbcType=INTEGER})");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", versionVo.getId());
		params.put("content", versionVo.getContent());
		params.put("version_no", versionVo.getVersionNo());
		params.put("pub_time", new Timestamp(versionVo.getPubTime().getTime()));
		params.put("update_content", versionVo.getUpdateContent());
		params.put("creater_id", versionVo.getCreaterId());
		params.put("creater_name", versionVo.getCreaterName());
		params.put("create_time", new Timestamp(new Date().getTime()));
		params.put("updater_id", versionVo.getCreaterId());
		params.put("updater_name", versionVo.getCreaterName());
		params.put("update_time", new Timestamp(new Date().getTime()));
		params.put("del_sign", 0);
		this.executeSql(sqlBuilder.toString(), params);
	}

	@Override
	public List<VersionVo> getNotice(String erpNo) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		StringBuilder sqlBuilder = new StringBuilder("select id,content,version_no,pub_time,update_content from ");
		sqlBuilder.append(" t_version where del_sign=0 ");
		sqlBuilder.append(" and pub_time > (IFNULL((select max(login_time) from t_login_mes where erp_no=?),'1900-01-01')) ");
		if (!StringUtil.isNullOrEmpty(erpNo)) {
			arrayList.add(erpNo);
		}
		sqlBuilder.append(" order by pub_time desc ");
		
		return this.findAllBySQL(VersionVo.class, sqlBuilder.toString(), null, false, arrayList.toArray());
	}

}
